<template>
    <div>
        <el-row>
            <el-col :offset="10" :span="4">
                <div :style="{height : '30vh'}"/>
                <div>
                    <el-card :style="{minWidth : '450px',minHeight : '100px',position: 'relative'}">
                        <LoginTable nextPage="/management"/>
                    </el-card>
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import LoginTable from '../components/LoginTable'

export default {
    name : "Login",
    created() {
        if(this.$cookies.isKey("token")) {
            this.loginByToken();
        }
    },
    computed: {
        token : {
            get : function () { 
                return this.$store.getters.token;
             },
             set : function (val) {
                 this.$store.commit("setToken",val);
             }
        },
        loginByTokenURL : function() {
            return this.$store.getters.getPathPrefix() + 'token-login';
        }
    },
    data() {
        return {
            
        }
    },
    methods: {
        loginByToken() {
            this.$axios.post(this.loginByTokenURL)
            .then(response => {// 成功的返回
                if(response.data.flg == true) {
                    this.$store.commit('setLoginStatus',true);
                    
                    this.$message({
                        message : '登录成功!',
                        type : 'success'
                    });
                    if(response.data.token != null) {
                        this.token = response.data.token;
                    }
                    this.$router.push("/management");
                }
            }).catch(err => {// 失败的返回
                this.$message.error(err.msg);
            }); 
        },
    },
    components: {
        LoginTable
    }
}
</script>

<style scoped>

</style>